/**
 * @description  跳转页面函数
 * @param url  跳转的页面路径
 * @param animate  跳转动画
 * @param type  跳转类型
 * @returns 跳转函数
 */

export const goPage = (
  url: string,
  type?: "navigateTo" | "redirectTo" | "reLaunch" | "switchTab",
  animate?:
    | "auto"
    | "none"
    | "slide-in-right"
    | "slide-in-left"
    | "slide-in-top"
    | "slide-in-bottom"
    | "fade-in"
    | "zoom-out"
    | "zoom-fade-out"
    | "pop-in"
) => {
  const options = {
    url,
    animationType: animate || "pop-in",
  };

  if (type === "navigateTo") {
    return uni.navigateTo(options);
  } else if (type === "redirectTo") {
    return uni.redirectTo(options);
  } else if (type === "reLaunch") {
    return uni.reLaunch(options);
  } else if (type === "switchTab") {
    return uni.switchTab(options);
  } else {
    return uni.navigateTo(options);
  }
};

export const goBack = () => {
  uni.navigateBack();
};

// 格式化价格，将整数部分和小数部分分离
export const formatPrice = (price: number | string) => {
  const priceStr = price.toString();
  // 如果价格中含有小数点
  if (priceStr.includes(".")) {
    const [integer, decimal] = priceStr.split(".");
    // 确保小数部分有两位
    const formattedDecimal = decimal.length === 1 ? `${decimal}0` : decimal;
    return {
      integer,
      decimal: formattedDecimal,
    };
  } else {
    // 没有小数点，添加.00
    return {
      integer: priceStr,
      decimal: "00",
    };
  }
};
